﻿using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;
using ZR.Service.Service.ProductionControl;

//创建时间：2025-02-23
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 零件批次管理
    /// </summary>
    [Verify]
    [Route("business/CksBatchofpart")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksBatchofpartController : BaseController
    {
        /// <summary>
        /// 零件批次管理接口
        /// </summary>
        private readonly ICksBatchofpartService _CksBatchofpartService;

        public CksBatchofpartController(ICksBatchofpartService CksBatchofpartService)
        {
            _CksBatchofpartService = CksBatchofpartService;
        }

        /// <summary>
        /// 查询零件批次管理列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "cksbatchofpart:list")]
        public IActionResult QueryCksBatchofpart([FromQuery] CksBatchofpartQueryDto parm)
        {
            var response = _CksBatchofpartService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询零件批次管理详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "cksbatchofpart:query")]
        public IActionResult GetCksBatchofpart(long Id)
        {
            var response = _CksBatchofpartService.GetInfo(Id);

            var info = response.Adapt<CksBatchofpartDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加零件批次管理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "cksbatchofpart:add")]
        [Log(Title = "零件批次管理", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksBatchofpart([FromBody] CksBatchofpartDto parm)
        {
            
            //查询批次号是否存在
           bool any = _CksBatchofpartService.Queryable().Where(a => a.BatchNo == parm.BatchNo).Any();
            if(any)
                return ToResponse(ResultCode.FAIL, "批次码已存在");
            //检查当前零件类型，是否有正在使用的批次，有则变更状态为已完成
            parm.StartTime = DateTime.Now;
            
            var modal = parm.Adapt<CksBatchofpart>().ToCreate(HttpContext);
            modal.Status = 1;
            modal.CreatedTime = DateTime.Now;
            var response = _CksBatchofpartService.AddCksBatchofpart(modal);

            return SUCCESS(response);
        }


        [HttpPost("endbatch")]
        [ActionPermissionFilter(Permission = "cksbatchofpart:edit")]
        [Log(Title = "完结零件批次", BusinessType = BusinessType.UPDATE)]
        public IActionResult Endbatch([FromBody] CksBatchofpartDto parm)
        {

            //查询批次号是否存在
            var one = _CksBatchofpartService.Queryable().Where(a => a.BatchNo == parm.BatchNo).Single();
            if (one == null)
                return ToResponse(ResultCode.FAIL, "批次不存在");

            one.EndTime = DateTime.Now;
            one.Status = 10;
            var response =_CksBatchofpartService.Update(one, (a =>new { a.EndTime,a.Status }));

            

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新零件批次管理
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "cksbatchofpart:edit")]
        [Log(Title = "零件批次管理", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksBatchofpart([FromBody] CksBatchofpartDto parm)
        {
            var modal = parm.Adapt<CksBatchofpart>().ToUpdate(HttpContext);
            var response = _CksBatchofpartService.UpdateCksBatchofpart(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除零件批次管理
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "cksbatchofpart:delete")]
        [Log(Title = "零件批次管理", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksBatchofpart([FromRoute] string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);

            return ToResponse(_CksBatchofpartService.Delete(idArr));
        }

    }
}